package com.niiwoo.civet.account.service.local.check.job.apiservice;

import com.niiwoo.civet.account.dao.entity.AccountBase;
import com.niiwoo.civet.account.dao.entity.AccountBaseExample;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAccountGuarantors;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAccountGuarantorsExample;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAccountStatistics;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAllbalance;
import com.niiwoo.civet.account.dao.entity.AccountCheckingAllbalanceExample;
import com.niiwoo.civet.account.dao.mapper.AccountBaseMapperExt;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingAccountGuarantorsMapper;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingAccountStatisticsMapperExt;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingAllbalanceMapperExt;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingCommissionMapperExt;
import com.niiwoo.civet.account.dao.mapper.AccountCheckingRollbackRechargeOrderMapperExt;
import com.niiwoo.civet.account.dto.request.AccountCheckingAllbalanceRequestDTO;
import com.niiwoo.tripod.lanmao.enums.PlateformAccountEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import com.niiwoo.tripod.lanmao.properties.LanMaoProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 对账统计
 */
@Slf4j
@Service
public class AccountCheckingStatisticsService {

    @Autowired
    private AccountCheckingAccountStatisticsMapperExt accountCheckingAccountStatisticsMapperExt;
    @Autowired
    private AccountCheckingRollbackRechargeOrderMapperExt accountCheckingRollbackRechargeOrderMapperExt;
    @Autowired
    private AccountCheckingAllbalanceMapperExt accountCheckingAllbalanceMapperExt;
    @Autowired
    private AccountCheckingCommissionMapperExt accountCheckingCommissionMapperExt;
    @Autowired
    private LanMaoProperties lanMaoProperties;

    @Autowired
    private AccountCheckingAccountGuarantorsMapper accountCheckingAccountGuarantorsMapper;
    @Autowired
    private AccountBaseMapperExt accountBaseMapperExt;


    public void accountStatistics(Long checkRecordId){

        AccountCheckingAccountStatistics accountCheckingAccountStatistics = null;
        accountCheckingAccountStatistics = accountCheckingAccountStatisticsMapperExt.selectByCheckingRecordId(checkRecordId);
        boolean isRecheck = Boolean.FALSE;
        if( accountCheckingAccountStatistics != null ){
             isRecheck = Boolean.TRUE ;
        }else{
            accountCheckingAccountStatistics = new AccountCheckingAccountStatistics();
        }
        accountCheckingAccountStatistics.setCheckingRecordId(checkRecordId);
        accountCheckingAccountStatistics.setPlatformManageFee(new BigDecimal("0"));

        this.totalPlatformAccount(checkRecordId,accountCheckingAccountStatistics);
        this.totalBackrollRecharge(checkRecordId,accountCheckingAccountStatistics);
        this.totalCommission(checkRecordId,accountCheckingAccountStatistics);
        if(isRecheck){
            accountCheckingAccountStatistics.setUpdateTime(new Date());
            accountCheckingAccountStatisticsMapperExt.updateByPrimaryKeySelective(accountCheckingAccountStatistics);
        }else{
            accountCheckingAccountStatistics.setCreateTime(new Date());
            accountCheckingAccountStatistics.setUpdateTime(new Date());
            accountCheckingAccountStatisticsMapperExt.insert(accountCheckingAccountStatistics);
        }

        log.info("平台期末余额统计,平台总账户、平台收入账户、平台营销款账户、担保公司账户、平台管理费、提现费、提现回退费 统计完成");
    }
    /**
     * 统计平台账户
     * @param checkRecordId
     */
    public void totalPlatformAccount(Long checkRecordId,AccountCheckingAccountStatistics accountCheckingAccountStatistics){
        String guaranteeUserId = lanMaoProperties.getNiiWooAccount().getGuaranteeUserId();
        String [] platformAccount = new String [] {PlateformAccountEnum.SYS_GENERATE_000.toString(),
                PlateformAccountEnum.SYS_GENERATE_002.toString(),
                PlateformAccountEnum.SYS_GENERATE_003.toString(),
                PlateformAccountEnum.SYS_GENERATE_004.toString(),
                guaranteeUserId};

        AccountCheckingAllbalanceRequestDTO accountCheckingAllbalanceRequestDTO = new AccountCheckingAllbalanceRequestDTO();
        accountCheckingAllbalanceRequestDTO.setCheckingRecordId(checkRecordId);
        accountCheckingAllbalanceRequestDTO.setUserNOs(Arrays.asList(platformAccount));
        List<AccountCheckingAllbalance> accountCheckingAllbalanceList = accountCheckingAllbalanceMapperExt.queryCheckingAllBalanceByUserNo(accountCheckingAllbalanceRequestDTO);

        for(AccountCheckingAllbalance accountCheckingAllbalance : accountCheckingAllbalanceList){
            if(PlateformAccountEnum.SYS_GENERATE_000.toString().equals(accountCheckingAllbalance.getPlatformUserNo())){  //平台管理
                accountCheckingAccountStatistics.setPlatformTotalAccount(accountCheckingAllbalance.getAmount());
                continue;
            }else if (PlateformAccountEnum.SYS_GENERATE_002.toString().equals(accountCheckingAllbalance.getPlatformUserNo())){
                accountCheckingAccountStatistics.setPlatformMarketingAccount(accountCheckingAllbalance.getAmount());
                continue;
            }else if (PlateformAccountEnum.SYS_GENERATE_003.toString().equals(accountCheckingAllbalance.getPlatformUserNo())){
                //accountCheckingAccountStatistics.setPlatformTotalAccount(accountCheckingAllbalance.getAmount());
                //continue;
            }else if (PlateformAccountEnum.SYS_GENERATE_004.toString().equals(accountCheckingAllbalance.getPlatformUserNo())){
                accountCheckingAccountStatistics.setPlatformIncomeAccount(accountCheckingAllbalance.getAmount());
                continue;
            }else if (guaranteeUserId.equals(accountCheckingAllbalance.getPlatformUserNo())){
                accountCheckingAccountStatistics.setPlatformOrgAccount(accountCheckingAllbalance.getAmount());
                continue;
            }
        }
    }

    /**
     * 统计平台当期资金回退
     * @param checkRecordId
     */
    public void totalBackrollRecharge(Long checkRecordId,AccountCheckingAccountStatistics accountCheckingAccountStatistics){
         BigDecimal totalBackrollRechargeAmount = accountCheckingRollbackRechargeOrderMapperExt.selectTotalRollbackRechargeAmount(checkRecordId);
         accountCheckingAccountStatistics.setPlatformWithdrawBackFee(totalBackrollRechargeAmount);
    }

    /**
     * 统计当期提现佣金
     * @param checkRecordId
     */
    public void totalCommission(Long checkRecordId,AccountCheckingAccountStatistics accountCheckingAccountStatistics){
        BigDecimal totalAmount = accountCheckingCommissionMapperExt.selectTotalWithdrawFeeAmount(checkRecordId);
        accountCheckingAccountStatistics.setPlatformWithdrawFee(totalAmount);
    }

    /**
     * 账期统计机构期末余额
     * @param checkRecordId 账期ID
     */
    public void accountGuarantors(Long checkRecordId){
        log.info("{}:机构对账期末统计开始",checkRecordId);
        AccountCheckingAccountGuarantorsExample accountCheckingAccountGuarantorsExample = new AccountCheckingAccountGuarantorsExample();
        AccountCheckingAccountGuarantorsExample.Criteria criteria = accountCheckingAccountGuarantorsExample.createCriteria();
        criteria.andCheckingRecordIdEqualTo(checkRecordId);
        accountCheckingAccountGuarantorsMapper.deleteByExample(accountCheckingAccountGuarantorsExample);

        AccountBaseExample accountBaseExample = new AccountBaseExample();
        AccountBaseExample.Criteria accountBaseCriteria = accountBaseExample.createCriteria();
        //accountBaseCriteria.andAccountRoleEqualTo(UserRoleEnum.GUARANTEECORP.getValue());
        accountBaseCriteria.andAccountRoleIn(Arrays.asList(UserRoleEnum.GUARANTEECORP.getValue(),UserRoleEnum.COLLABORATOR.getValue()));
        List<AccountBase> accountBaseList = accountBaseMapperExt.selectByExample(accountBaseExample);
        List<String> userIds = new ArrayList();
        accountBaseList.stream().forEach(rs->{
            userIds.add(rs.getAccountNo());
        });

        AccountCheckingAllbalanceExample accountCheckingAllbalanceExample = new AccountCheckingAllbalanceExample();
        AccountCheckingAllbalanceExample.Criteria accountCheckingAllbalanceCriteria = accountCheckingAllbalanceExample.createCriteria();
        accountCheckingAllbalanceCriteria.andPlatformUserNoIn(userIds);
        accountCheckingAllbalanceCriteria.andCheckingRecordIdEqualTo(checkRecordId);
        List<AccountCheckingAllbalance> accountCheckingAllbalanceList = accountCheckingAllbalanceMapperExt.selectByExample(accountCheckingAllbalanceExample);
        accountCheckingAllbalanceList.stream().forEach(rs->{
            AccountCheckingAccountGuarantors accountCheckingAccountGuarantors = new AccountCheckingAccountGuarantors();
            accountCheckingAccountGuarantors.setCheckingRecordId(checkRecordId);
            accountCheckingAccountGuarantors.setAmount(rs.getAmount());
            accountCheckingAccountGuarantors.setPlatformUserNo(rs.getPlatformUserNo());
            accountCheckingAccountGuarantors.setCreateTime(new Date());
            accountCheckingAccountGuarantors.setUpdateTime(new Date());
            accountCheckingAccountGuarantorsMapper.insert(accountCheckingAccountGuarantors);
        });
        log.info("{}:机构对账期末统计结束",checkRecordId);
    }

}
